import React from 'react';
import { inject, observer } from 'mobx-react';
import { WeaErrorPage, WeaTools } from 'ecCom';
import { WeaLocaleProvider } from 'ecCom';
import FormTab from './FormTab';

const getLabel = WeaLocaleProvider.getLabel;

@inject('sellchanceViewStore')
@observer
class SellchanceView extends React.Component {
  constructor(props) {
    super(props);
  }

  componentDidMount() {
    const { location: { query }, sellchanceViewStore } = this.props;
    const { initSellchanceInfo } = sellchanceViewStore;
    initSellchanceInfo(query);
  }

  componentWillReceiveProps(nextProps) {
    const keyOld = this.props.location.key;
    const keyNew = nextProps.location.key;
    // 点击菜单路由刷新组件
    if (keyOld !== keyNew) {
      const { sellchanceViewStore, location: { query } } = nextProps;
      const { initSellchanceInfo } = sellchanceViewStore;
      initSellchanceInfo(query);
    }
  }

  render() {
    const { location } = this.props;
    const { customerId, sellchanceId } = this.props.sellchanceViewStore;
    if (sellchanceId && customerId) {
      return <FormTab ecId={`${this && this.props && this.props.ecId || ''}_FormTab@zwacjt`} location={location} params={{ customerid: customerId, selectedKey: 0, sellChanceId: sellchanceId }} />;
    }
    return <div />;
  }
}

export default WeaTools.tryCatch(React,
  props => <WeaErrorPage ecId={`${this && this.props && this.props.ecId || ''}_WeaErrorPage@chqu4e`} msg={props.error ? props.error : getLabel(129417, '服务器运行出错，请联系系统管理员！')} />,
  { error: '' },
)(SellchanceView);
